import vue from 'vue'
import VueRouter from 'vue-router';
import Home from '@/page/Home';
import Login from '@/page/Login';
import Register from '@/page/Register';
import Search from '@/page/Search';
import Detail from '@/page/Detail';
import AddCartSuccess from '@/page/AddCartSuccess';
import ShopCart from '@/page/ShopCart';
import Trade from '@/page/Trade';
import Pay from '@/page/Pay';
import PaySuccess from '@/page/PaySuccess';
import Center from '@/page/Center';
import MyOrder from '@/page/Center/MyOrder';
import TeamOrder from '@/page/Center/TeamOrder';
import store from '@/store'

var originPush = VueRouter.prototype.push
var originReplace = VueRouter.prototype.replace

//重写push/replace方法解决
VueRouter.prototype.push = function (location, resolve, reject) {
    if (reject && resolve) {
        originPush.call(this, location, resolve, reject)
    } else {
        originPush.call(this, location, () => { }, () => { })
    }
}

VueRouter.prototype.replace = function (location, resolve, reject) {
    if (reject && resolve) {
        originReplace.call(this, location, resolve, reject)
    } else {
        originReplace.call(this, location, () => { }, () => { })
    }
}

vue.use(VueRouter)

let router = new VueRouter({
    routes: [
        {
            name: 'home',
            path: '/home',
            component: Home,
            meta: {
                showFooter: true
            }
        },
        {
            name: 'login',
            path: '/login',
            component: Login,
            meta: {
                showFooter: false
            }
        },
        {
            name: 'register',
            path: '/register',
            component: Register,
            meta: {
                showFooter: false
            }
        },
        {
            name: 'search',
            path: '/search/:keyword?',
            component: Search,
            props: true,
            meta: {
                showFooter: true
            }
        },
        {
            name: 'detail',
            path: '/detail/:id?',
            component: Detail,
            props: true,
            meta: {
                showFooter: true
            }
        },
        {
            name: 'addCartSuccess',
            path: '/addCartSuccess',
            component: AddCartSuccess,
        },
        {
            name: 'shopCart',
            path: '/shopCart',
            component: ShopCart,
        },
        {
            name: 'Trade',
            path: '/trade',
            component: Trade,
        },
        {
            name: 'pay',
            path: '/pay',
            component: Pay,
        },
        {
            name: 'paysuccess',
            path: '/paysuccess',
            component: PaySuccess,
        },
        {
            name: 'center',
            path: '/center',
            component: Center,
            children: [
                {
                    name: 'myOrder',
                    path: 'myOrder',
                    component: MyOrder,
                },
                {
                    name: 'teamOrder',
                    path: 'teamOrder',
                    component: TeamOrder,
                },
            ]
        },
        {
            path: '*',
            redirect: '/home'
        },
    ],
    scrollBehavior(to, from, savedPosition) {
        // return 期望滚动到哪个的位置
        return { y: 0 }
    }
})

router.beforeEach(async (to, from, next) => {
    let token = store.state.user.token
    let user = store.state.user.userInfo
    if (token) {
        if (to.path == '/login' || to.path == '/register') {
            next(from.path)
        } else {
            if (user?.name) {
                next()
            } else {
                try {
                    await store.dispatch('user/getUserInfo')
                    next()
                } catch (error) {
                    await store.dispatch('user/logout')
                    next()
                }
            }
        }
    } else {
        console.log(to, from)
        if (to.path.indexOf('/trade') != -1 || to.path.indexOf('/pay') != -1 || to.path.indexOf('/center') != -1) {
            next(`/login?redirct=${to.name}`)
        }
        if (to.path.indexOf('/login') != -1 && !to.query.redirct) {
            next(`/login?redirct=${from.name}`)
        }
        next()

    }

})

export default router